//
//  ContactHandler.m
//  iFly
//
//  Created by User-10 on 12/8/14.
//  Copyright (c) 2014 TeamDifferent. All rights reserved.
//

#import "ContactHandler.h"
#import "CharacterNode.h"
#import "EnemyNode.h"
#import "BonusNode.h"
#import "ProjectileNode.h"

@implementation ContactHandler

+(void) contactBetweenNodeA:(SKSpriteNode*) firstBody andNodeB:(SKSpriteNode*) secondBody {
    if ((firstBody.physicsBody.categoryBitMask & obstacleCategory) != 0) {
        //enemy collides with projectile
        if ((secondBody.physicsBody.categoryBitMask & weaponsCategory) != 0) {
            [self projectile:(ProjectileNode*)secondBody didCollideWithEnemy:(EnemyNode*)firstBody];
        }
        if ((secondBody.physicsBody.categoryBitMask & shieldCategory) != 0) {
            [self enemy:(EnemyNode*)firstBody didCollideWithShield:secondBody];
        }
    }
    
    
    //character collides with:
    if ((firstBody.physicsBody.categoryBitMask & characterCategory) != 0) {
        //colliding with obstacle
        if ((secondBody.physicsBody.categoryBitMask & obstacleCategory) != 0) {
            [self character:(CharacterNode*)firstBody didCollideWithEnemy:(EnemyNode*)secondBody];
        }
        //colliding with coin
        else if ((secondBody.physicsBody.categoryBitMask & bonusCategory) != 0) {
            [self character:(CharacterNode*)firstBody didCollideWithBonus:(BonusNode*)secondBody];
        }
    }
    
    //shielded character collides with bonus
    if ((firstBody.physicsBody.categoryBitMask & shieldCategory) != 0) {
        if ((secondBody.physicsBody.categoryBitMask & bonusCategory) != 0) {
            [self character:(CharacterNode*)firstBody didCollideWithBonus:(BonusNode*)secondBody];
        }
    }
}

+(void) projectile:(ProjectileNode*) projectile didCollideWithEnemy:(EnemyNode*) enemy {
    for (int i = 0; i < projectile.damage; i++) {
        [enemy wasHit];
    }
    [projectile didHit];
    
}

+(void) character:(CharacterNode*) character didCollideWithEnemy:(EnemyNode*) enemy {
    [character wasHit];
    [enemy removeFromParent];
}

+(void) character:(CharacterNode*) character didCollideWithBonus:(BonusNode*) bonus {
    [bonus wasTaken];
    
}

+(void) enemy:(EnemyNode*) enemy didCollideWithShield:(SKSpriteNode*) shield {
    //we imagine the shield is super overpowered and it has infinite damage
    [enemy wasHit];
    if (enemy != nil) {
        CharacterNode* character = (CharacterNode*)[enemy.parent childNodeWithName:@"character"];
        [character addScore:enemy.pointsForKill];
        [enemy removeFromParent];
    }
    //to add sparkly effects on shield and sounds
}
//adding sounds and score

@end
